package com.hanxy.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanxy.admin.entity.SysDepartment;
import com.hanxy.admin.entity.SysRole;
import com.hanxy.admin.exception.ZbbWebException;
import com.hanxy.admin.mapper.SysDepartmentMapper;
import com.hanxy.admin.model.req.SysDepartmentReq;
import com.hanxy.admin.model.resp.SysDepartmentResp;
import com.hanxy.admin.redis.RedissonService;
import com.hanxy.admin.service.ISysDepartmentService;
import com.hanxy.admin.service.ISysRoleService;
import com.hxy.base.utils.JsonUtils;
import org.redisson.api.RMapCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 系统部门Service实现
 **/
@Service
public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper, SysDepartment> implements ISysDepartmentService {

    @Autowired
    private SysDepartmentMapper sysDepartmentMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private RedissonService redissonService;

    /**
     * [获取分页列表]
     */
    @Override
    public IPage<SysDepartmentResp> getPageList(SysDepartmentReq req) {
        return sysDepartmentMapper.getPageList(new Page<>(req.getPageNo(), req.getPageSize()), req);
    }

    /**
     * [获取列表]
     */
    @Override
    public List<SysDepartmentResp> getList(SysDepartmentReq req) {
        List<SysDepartmentResp> sysDepartmentRespList = null;
        RMapCache<String, String> jobMap = redissonService.getRMapCache("ADMIN");
        if (jobMap.containsKey("department:getList")) {
            String string = jobMap.get("department:getList");
            sysDepartmentRespList = JsonUtils.jsonToList(string, SysDepartmentResp.class);
        } else {
            List<SysDepartment> sysDepartmentList = list(new LambdaQueryWrapper<SysDepartment>().eq(SysDepartment::getParentId, 0));
            sysDepartmentRespList = BeanUtil.copyToList(sysDepartmentList, SysDepartmentResp.class);
            sysDepartmentRespList.forEach(o -> getChildren(o.getId(), o));
            jobMap.put("department:getList", JsonUtils.objectToJson(sysDepartmentRespList), 30, TimeUnit.MINUTES);
        }

        return sysDepartmentRespList;
    }

    /**
     * [详情]
     */
    @Override
    public SysDepartmentResp getDetail(Integer id) {
        SysDepartment sysDepartment = getById(id);
        if (null != sysDepartment) {
            SysDepartmentResp sysDepartmentResp = BeanUtil.copyProperties(sysDepartment, SysDepartmentResp.class);
            return sysDepartmentResp;
        } else {
            throw new ZbbWebException("部门参数有误或不存在！");
        }
    }

    /**
     * [新增更新]
     */
    @Override
    public void addOrUpdate(SysDepartmentReq req) {
        int count = count(new LambdaQueryWrapper<SysDepartment>()
                .eq(SysDepartment::getName, req.getName())
                .ne(null != req.getId(), SysDepartment::getId, req.getId()));
        if (count > 0) {
            throw new ZbbWebException("部门名称重复！");
        }
        SysDepartment sysDepartment = BeanUtil.copyProperties(req, SysDepartment.class);
        saveOrUpdate(sysDepartment);
    }

    /**
     * [删除]
     */
    @Override
    public void delete(Integer id) {
        int count = count(new LambdaQueryWrapper<SysDepartment>().eq(SysDepartment::getParentId, id));
        if (count > 0) {
            throw new ZbbWebException("存在下级部门，不可删除！");
        }
        int roleCount = sysRoleService.count(new LambdaQueryWrapper<SysRole>().eq(SysRole::getDepartmentId, id));
        if (roleCount > 0) {
            throw new ZbbWebException("部门下存在角色，不可删除！");
        }
        removeById(id);
    }

    /**
     * [获取子集]
     */
    private void getChildren(Integer parentId, SysDepartmentResp resp) {
        List<SysDepartment> list = list(new LambdaQueryWrapper<SysDepartment>()
                .eq(SysDepartment::getParentId, parentId));
        if (!list.isEmpty()) {
            List<SysDepartmentResp> sysDepartmentRespList = BeanUtil.copyToList(list, SysDepartmentResp.class);
            resp.setChildren(sysDepartmentRespList);
            sysDepartmentRespList.forEach(o -> getChildren(o.getId(), o));
        }
    }

}
